package jeons.admin.product.biz.impl;

import java.util.Iterator;

import java.util.List;

import jeons.admin.product.biz.PowerBiz;
import jeons.admin.product.biz.ProductBiz;
import jeons.admin.product.biz.ProductImgBiz;
import jeons.admin.product.dto.PowerDTO;
import jeons.admin.product.dto.ProductDTO;
import jeons.admin.product.dto.ProductImgDTO;
import jeons.common.util.FileService;
import jeons.common.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional
public class PowerBizImpl extends SqlMapClientDaoSupport implements PowerBiz{
	@Autowired SqlMapClientTemplate sqlMapClientTemplate;
	@Autowired FileService fileService;
	@Autowired ProductImgBiz productImgBiz;
	@Autowired ProductBiz productBiz;
	
	@Override
	public PowerDTO insertData(PowerDTO powerDTO, List listimg, List dteilimg) {
		ProductDTO productDTO = new ProductDTO();
		productDTO.setPrice(powerDTO.getPrice());
		productDTO.setType(powerDTO.getType());
		productDTO.setP_number(powerDTO.getP_number());
		productDTO.setProduct(powerDTO.getProduct());
		productDTO.setRgdate(powerDTO.getRgdate());
		productDTO.setManufacturer(powerDTO.getManufacturer());
		
		sqlMapClientTemplate.insert("Product.insertData",productDTO);
		powerDTO.setProno(productDTO.getProno());
		sqlMapClientTemplate.insert("Power.insertData",powerDTO);
		
		String today = Utils.getYmdhhmmss("yyyyMMddhhmmss");
		ProductImgDTO productImgDTO = new ProductImgDTO();
		for (Iterator iterator = dteilimg.iterator();  iterator.hasNext();) {
			MultipartFile multipartFile = (MultipartFile)iterator.next();
			fileService.uploadData(multipartFile,"product",today);
			productImgDTO.setProno(productDTO.getProno());		// ��ǰ���̺� �ڵ�
			productImgDTO.setType(productDTO.getType());
			productImgDTO.setB_filename(today+multipartFile.getOriginalFilename());
			productImgDTO.setB_realname(multipartFile.getOriginalFilename());
			productImgDTO.setB_filesize((int)multipartFile.getSize());
			//System.out.println("listimg :"+multipartFile.getOriginalFilename());;
			
			
		}	//����
		for (Iterator iterator =  listimg.iterator();  iterator.hasNext();) {
			MultipartFile multipartFile = (MultipartFile)iterator.next();
			fileService.uploadData(multipartFile,"product",today);
			productImgDTO.setL_filename(today+multipartFile.getOriginalFilename());
			productImgDTO.setL_realname(multipartFile.getOriginalFilename());
			productImgDTO.setL_filesize((int)multipartFile.getSize());
			
		}
		productImgBiz.uploadImgData(productImgDTO);
		return powerDTO;
	}
	@Override
	public PowerDTO getData(int prono) {
		// TODO Auto-generated method stub
		return (PowerDTO) sqlMapClientTemplate.queryForObject("Power.getData",prono);
	}
	@Override
	public void updateData(PowerDTO powerDTO) {
		ProductDTO productDTO = new ProductDTO();
		productDTO.setPrice(powerDTO.getPrice());
		productDTO.setType(powerDTO.getType());
		productDTO.setP_number(powerDTO.getP_number());
		productDTO.setProduct(powerDTO.getProduct());
		productDTO.setRgdate(powerDTO.getRgdate());
		productDTO.setManufacturer(powerDTO.getManufacturer());
		productDTO.setProno(powerDTO.getProno());
		productBiz.updateData(productDTO);
		powerDTO.setProno(productDTO.getProno());
		sqlMapClientTemplate.update("Power.updateData",powerDTO);
		
	}
	@Override
	public PowerDTO getALLData(int prono) {
		return (PowerDTO) sqlMapClientTemplate.queryForObject("Power.getALLData",prono);
	}
}
